/*
 * Copyright (C) 2010 by Lei Shi (sjtushi@gmail.com)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License, 
 * version 2, or any later versions.
 */

/*
 * Implement a simple stack used to trace directory tree.
 */

#include "stack.h"

int init_stack(stack_t *stack) {
   CHECK_NULL_PTR(stack);
   stack->top = 0;
   memset(stack->dir_stack, 0, STACK_SIZE*STACK_STR_LENGTH);
   return STATUS_SUCCESS;
}

int push_stack(stack_t *stack, char* str) {
   if (stack == NULL || str == NULL)
      return STATUS_ERROR;

   if (isfull_stack(stack)) {
      printf("Stack is full\n");
      return STATUS_ERROR;
   }
   strcpy(stack->dir_stack[stack->top++], str);
   return STATUS_SUCCESS;
}

char* pop_stack(stack_t *stack) {
   if (stack == NULL)
      return NULL;
   if (isempty_stack(stack)) {
      printf("Stack is empty\n");
      return NULL;
   }
   return stack->dir_stack[--(stack->top)];
}

int isempty_stack(stack_t *stack) {
   return (stack->top == 0);
}

int isfull_stack(stack_t *stack) {
   return (stack->top == 10);
}

